<template>
  <text
    v-bind="allCompAttrs"
  >
    {{ formatterValue }}
  </text>
</template>
<script>
export default {
  inheritAttrs: false,
  name:"DismensionGdText"
};
</script>
<script setup>
import { computed } from "vue";
import useComponentAttrs, { embeddedAtomicCompProps } from "../../useComponentAttrs";

const props = defineProps({
  ...embeddedAtomicCompProps,
});
//组件所有属性的默认值映射
const compAttrsKeyDefaultValueMapping = {
  style: {},
};
//组件属性继承属性的key
const compAttrsInheritedAttrKeys = [];

const { curBindVal, containerType, allCompAttrs } = useComponentAttrs({
  props,
  compAttrsKeyDefaultValueMapping,
  compAttrsInheritedAttrKeys,
});
const getFormatterFunc = computed(() => {
  const formatterVal = props.metaItem?.formatterFunc;
  if (typeof formatterVal === "string") {
    return new Function("configObj", formatterVal);
  } else if (typeof formatterVal === "function") {
    return formatterVal;
  } else {
    return ({
      rowIdx,
      colIdx,
      $rowIdx,
      $coldx,
      metaKey,
      metaItem,
      metaItemList,
      rowData,
      formData,
      tableBodyData,
    }) =>
      containerType.value === "Table"
        ? rowData[metaItem.key]
        : formData[metaItem.key];
  }
});
//格式化值 仅文本类型需要使用，其他Input、Switch、Select等表单组件 不需要

//TODO:FIXME:优化性能,静态数据避免动态响应
const getStaticBind_formatterValue = () => {
  const formatterFunc = getFormatterFunc.value;
  return formatterFunc({
    formData: props.formData,
    rowData: props.rowData,
    metaItem: props.metaItem,
    rowIdx: props.rowIdx,
    colIdx: props.colIdx,
    //$rowIdx,
    //$coldx,
    metaKey: props.metaItem.key,
    metaItemList: props.metaItemList,
    tableBodyData: props.tableBodyData,
  });
};

const getDynmicBind_formatterValue = () =>
  computed(() => {
    const formatterFunc = getFormatterFunc.value;
    return formatterFunc({
      formData: props.formData,
      rowData: props.rowData,
      metaItem: props.metaItem,
      rowIdx: props.rowIdx,
      colIdx: props.colIdx,
      //$rowIdx,
      //$coldx,
      metaKey: props.metaItem.key,
      metaItemList: props.metaItemList,
      tableBodyData: props.tableBodyData,
    });
  });
const formatterValue =
  props.commonAttr?.performance?.isStaticBind ?? false
    ? getStaticBind_formatterValue()
    : getDynmicBind_formatterValue();
</script>
<style lang="scss" scoped></style>
